package com.un.ebs.inventory.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.un.ebs.inventory.domain.Inventory;
import com.un.ebs.inventory.dto.JxcVo;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.mapping.StatementType;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author liubo
 * @since 2020-05-23
 */
@Component
public interface InventoryMapper extends BaseMapper<Inventory> {

    /**
     * 入库，并释放锁
     *
     * @param warehouseId
     * @param skuId
     * @param qty           入库数量
     * @param sourceQty     原库存量
     * @param sourceLockQty 释放锁定数量
     * @return
     */
    @Update("update tb_inventory set qty=qty+#{qty},lock_by_in_qty=lock_by_in_qty-#{qty} where warehouse_id=#{warehouseId} and sku_id=#{skuId} and qty=#{sourceQty} and lock_by_in_qty=#{sourceLockQty}")
    int plusLockRelease(@Param("warehouseId") String warehouseId, @Param("skuId") String skuId, @Param("qty") Double qty, @Param("sourceQty") Double sourceQty, @Param("sourceLockQty") Double sourceLockQty);

    @Update("update tb_inventory set qty=qty+#{qty} where warehouse_id=#{warehouseId} and sku_id=#{skuId} and qty=#{sourceQty}")
    int plus(@Param("warehouseId") String warehouseId, @Param("skuId") String skuId, @Param("qty") Double qty, @Param("sourceQty") Double sourceQty);

    /**
     * 入库锁
     *
     * @param warehouseId
     * @param skuId
     * @param qty         入库数量
     * @param sourceQty   原库存数量
     * @return
     */
    @Update("update tb_inventory set lock_by_in_qty=lock_by_in_qty+#{qty} where warehouse_id=#{warehouseId} and sku_id=#{skuId} and lock_by_in_qty=#{sourceQty}")
    int plusLock(@Param("warehouseId") String warehouseId, @Param("skuId") String skuId, @Param("qty") Double qty, @Param("sourceQty") Double sourceQty);

    /**
     * 出库，并释放出库锁
     *
     * @param warehouseId
     * @param skuId
     * @param qty           出库数量
     * @param sourceQty     原锁定库存量
     * @param sourceLockQty 释放锁定数量
     * @return
     */
    @Update("update tb_inventory set qty=qty-#{qty},lock_by_out_qty=lock_by_out_qty-#{qty} where warehouse_id=#{warehouseId} and sku_id=#{skuId} and qty=#{sourceQty} and lock_by_out_qty=#{sourceLockQty}")
    int reduceLockRelease(@Param("warehouseId") String warehouseId, @Param("skuId") String skuId, @Param("qty") Double qty, @Param("sourceQty") Double sourceQty, @Param("sourceLockQty") Double sourceLockQty);

    @Update("update tb_inventory set qty=qty-#{qty} where warehouse_id=#{warehouseId} and sku_id=#{skuId} and qty=#{sourceQty}")
    int reduce(@Param("warehouseId") String warehouseId, @Param("skuId") String skuId, @Param("qty") Double qty, @Param("sourceQty") Double sourceQty);

    /**
     * 出库锁
     *
     * @param warehouseId
     * @param skuId
     * @param qty         出库数量
     * @param sourceQty   原锁定出库量
     * @return
     */
    @Update("update tb_inventory set lock_by_out_qty=lock_by_out_qty+#{qty} where warehouse_id=#{warehouseId} and sku_id=#{skuId} and lock_by_out_qty=#{sourceQty}")
    int reduceLock(@Param("warehouseId") String warehouseId, @Param("skuId") String skuId, @Param("qty") Double qty, @Param("sourceQty") Double sourceQty);

    @Select("select * from dbo.fun_get_jxc(#{year}, #{month},#{tenantId}) where sku_code=#{skuCode}")
    List<JxcVo> queryJxcBySkuCode(@Param("skuCode") String skuCode, @Param("year") int year, @Param("month") int month, @Param("tenantId") String tenantId);

    @Select("exec sp_query_jxc @tenantId=#{tenantId},@pageSize=#{pageSize},@pageIndex=#{pageIndex},@skuCode=#{skuCode}")
    @Options(statementType = StatementType.CALLABLE)
    List<JxcVo> queryJxc(@Param("skuCode") String skuCode, @Param("pageSize") int pageSize, @Param("pageIndex") int pageIndex, @Param("tenantId") String tenantId);

}
